package com.archstoday.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.adapter.MyFollowAdapter;
import com.archstoday.common.AsRes;
import com.archstoday.common.SpfUtils;
import com.archstoday.entity.BaseEntity;
import com.archstoday.entity.FollowListEntity;
import com.archstoday.utils.OAuthUtils;
import com.archstoday.utils.ToolUtils;
import com.archstoday.view.PullToRefreshLayout;
import com.base.http.common.BaseHttpClient;
import com.base.utils.MySharedPreferencesMgr;
import com.xw.repo.refresh.PullListView;

/**
 * 我的关注
 */
public class MyFollowActivity extends AsScrollBaseActivity implements PullToRefreshLayout.OnRefreshListener {

    private TextView mTitleTv;
    private PullListView mList;
    private PullToRefreshLayout mPullLayout;

    private MyFollowAdapter mAdapter;
    private int mIndex;
    private int page;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_my_follow);
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mList = (PullListView) findViewById(R.id.pull_list);
        mPullLayout = (PullToRefreshLayout) findViewById(R.id.pull_layout);
        mPullLayout.setOnRefreshListener(this);
    }

    @Override
    protected void initData(Bundle bundle) {
        mTitleTv.setText(R.string.my_follow_title);

        mAdapter = new MyFollowAdapter(mContext, click);
        mList.setAdapter(mAdapter);
        page = 1;
        upData();
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.back_btn);

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Intent intent = new Intent(mContext, UserInfoActivity.class);
//                intent.putExtra("isMe", false);
//                intent.putExtra("userId", mAdapter.getItem(i).getUserId());
//                startActivity(intent);
            }
        });
    }

    @Override
    public void treatClickEvent(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                appFinish();
                break;
            case R.id.item_cancel_btn:
                mIndex = (int) view.getTag();
                focus(mAdapter.getItem(mIndex).getUserId());
                break;
        }
    }

    private void focus(int userId) {
        initParameter();
        mParams.put("userIdLiked", userId);
        mParams.put("userId", MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""));
        mParams.put("token", MySharedPreferencesMgr.getString(SpfUtils.USER_TOKEN, ""));
        mParams.put("plat", "android");
        mParams.put("ifa", mApp.getDeviceID(mContext));
        mParams.put("action", "del");

        sendEncryptPostRequest(AsRes.getInstance().getUrl(AsRes.FOCUS_AUTHOR),
                AsRes.FOCUS_AUTHOR, mParams, BaseEntity.class);
    }

    private void upData() {
        initParameter();
        mParams.put("userId", MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""));
        mParams.put("token", MySharedPreferencesMgr.getString(SpfUtils.USER_TOKEN, ""));
        mParams.put("plat", "android");
        mParams.put("ifa", mApp.getDeviceID(mContext));
        mParams.put("page", page);
        mParams.put("pageSize", 10);

        sendEncryptPostRequest(AsRes.getInstance().getUrl(AsRes.FOLLOW_LIST),
                AsRes.FOLLOW_LIST, mParams, FollowListEntity.class);
    }

    @Override
    protected void onSuccess(String content, BaseHttpClient client, Object parse) {
        mPullLayout.refreshFinish(true);
        mPullLayout.loadMoreFinish(true);
        if (client.getUrlIdentifier() == AsRes.FOLLOW_LIST) {
            FollowListEntity entity = (FollowListEntity) parse;
            if (ToolUtils.isSuccess(entity.getStatus())) {
                if (entity.getData() != null && entity.getData().size() > 0) {
                    if (page == 1) {
                        mAdapter.setData(entity.getData());
                    } else {
                        mAdapter.addData(entity.getData());
                    }
                    page++;
                }
            } else if (entity.getStatus() == 10005) {
                MySharedPreferencesMgr.setString(SpfUtils.USER_ID, "");
                MySharedPreferencesMgr.setString(SpfUtils.USER_TOKEN, "");
                OAuthUtils.getInstance().cleanUserInfo();

                showToast("登录信息已过期，请重新登录");
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
            }
        } else if (client.getUrlIdentifier() == AsRes.FOCUS_AUTHOR) {
            BaseEntity entity = (BaseEntity) parse;
            if (ToolUtils.isSuccess(entity.getStatus())) {
                mAdapter.removeData(mIndex);
                showToast("取消关注成功");
            } else if (entity.getStatus() == 10005) {
                MySharedPreferencesMgr.setString(SpfUtils.USER_ID, "");
                MySharedPreferencesMgr.setString(SpfUtils.USER_TOKEN, "");
                OAuthUtils.getInstance().cleanUserInfo();

                showToast("登录信息已过期，请重新登录");
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
            } else {
                showToast(entity.getMsg());
            }
        }
    }

    @Override
    protected void onError(Throwable error, BaseHttpClient client) {
        new Thread() {
            @Override
            public void run() {
                new Handler(Looper.getMainLooper()).post(runnable);
            }
        }.start();
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            mPullLayout.refreshFinish(true);
            mPullLayout.loadMoreFinish(true);

            if (mAdapter.getCount() == 0) {
                mList.setPullUpEnable(false);
            }

            showToast(R.string.intent_error);
        }
    };

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        page = 1;
        upData();
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        upData();
    }
}